#include "ZoneManager.h"
#include "Zone.h"

ZoneManager* ZoneManager::m_Instance = NULL;

ZoneManager::ZoneManager(void)
{
	m_WorldZones.RemoveAll();
	m_WorldHeight = 0;
	m_WorldWidth = 0;
}

ZoneManager::~ZoneManager(void)
{
	Shutdown();
}

ZoneManager* ZoneManager::GetInstance(void)
{
	if( !m_Instance )
		m_Instance = new ZoneManager();

	return m_Instance;
}

void ZoneManager::Initialize(void)
{
	m_WorldZones.RemoveAll();
	m_WorldHeight = 0;
	m_WorldWidth = 0;
}

void ZoneManager::CreateWorld(void)
{
	int worldSize = m_WorldHeight*m_WorldWidth;

	if( worldSize <= 0 )
		return;

	Zone* newZone = NULL;

	for(unsigned int iter = 0; iter < (unsigned)worldSize; iter++)
	{
		newZone = new Zone();
		newZone->SetZoneID(0);
		newZone->SetZoneType(ZONE_MOUTAIN);

		m_WorldZones.AddBack(newZone);
		newZone = NULL;
	}

	for(unsigned int iter = 0; iter < (unsigned)worldSize; iter++)
	{
		if( !(iter%m_WorldWidth) )
		{
			m_WorldZones[iter]->SetNeighbor(NULL, WEST);
			m_WorldZones[iter]->SetNeighbor(m_WorldZones[iter+1], EAST);
		}
		else if( iter%m_WorldWidth == m_WorldWidth-1 )
		{
			m_WorldZones[iter]->SetNeighbor(m_WorldZones[iter-1], WEST);
			m_WorldZones[iter]->SetNeighbor(NULL, EAST);
		}
		else
		{
			m_WorldZones[iter]->SetNeighbor(m_WorldZones[iter-1], WEST);
			m_WorldZones[iter]->SetNeighbor(m_WorldZones[iter+1], EAST);
		}

		if( iter == 0 || iter == m_WorldWidth-1 )
		{
			m_WorldZones[iter]->SetNeighbor(NULL, NORTH);
			m_WorldZones[iter]->SetNeighbor(m_WorldZones[iter+m_WorldWidth], SOUTH);
		}
		else if( iter/m_WorldHeight == m_WorldWidth || iter == (unsigned(worldSize)-1) )
		{
			m_WorldZones[iter]->SetNeighbor(m_WorldZones[iter-m_WorldWidth], NORTH);
			m_WorldZones[iter]->SetNeighbor(NULL, SOUTH);
		}
		else
		{
			m_WorldZones[iter]->SetNeighbor(m_WorldZones[iter-m_WorldWidth], NORTH);
			m_WorldZones[iter]->SetNeighbor(m_WorldZones[iter+m_WorldWidth], SOUTH);
		}
	}
}

void ZoneManager::Shutdown(void)
{
	Zone* zoneTemp;
	for(unsigned int iter = 0; iter < m_WorldZones.Size(); iter++)
	{
		zoneTemp = m_WorldZones[iter];
		delete zoneTemp;
	}
	m_WorldZones.RemoveAll();
}

// Mutators
void ZoneManager::SetWorldSize(int width, int height)
{
	m_WorldWidth = width;
	m_WorldHeight = height;
}

// Accessors
Zone* ZoneManager::GetZone(int index)
{
	if( index < 0 || ((unsigned int)index) >= m_WorldZones.Size() )
		return NULL;

	return m_WorldZones[index];
}

unsigned int ZoneManager::GetWorldSize(void)
{
	unsigned int width, height;
	width = m_WorldWidth;
	height = m_WorldHeight;

	return (width*height);
}

unsigned int ZoneManager::GetWorldWidth(void)
{
	return m_WorldWidth;
}

unsigned int ZoneManager::GetWorldHeight(void)
{
	return m_WorldHeight;
}

void ZoneManager::SetWaterLimit()
{
	m_WaterLimit = (m_WorldHeight / 3) * (m_WorldWidth / 3);
}

void ZoneManager::AddToWaterLimit()
{
	m_WaterLimit--;
}

unsigned int ZoneManager::GetWaterLimit()
{
	return m_WaterLimit;
}